package org.zk.dubbo_provider.utils;

import org.springframework.stereotype.Component;
import org.zk.entity.Order;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 姚家乐
 * 2019-12-16 12:20
 * 财务计算教师的账目工具包
 */
@Component
public class AdminBillUtil {
    public Map acCount(List<Order> orderList,Double teahcerprice) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//时间的转换字符串
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date();//得到当前的时间戳
        Calendar calendar = Calendar.getInstance();//对月份进行操作
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        int ordercount = orderList.size();//得到该教师的订单的总数

        int ordercountbystate = 0;//得到该教师已经支付的订单的总数量

        double ordertotal = 0.00;//该教师学生已支付的订单总额
        Double ordertotal1;//该教师的已得总收益
        if (teahcerprice == null){
            ordertotal1 = 0.00;
        }else {
            ordertotal1 = teahcerprice;
        }


        int ordercountbytime = 0;//得到本月的订单数

        int ordercountbytime1 = 0;//得到本月已支付的订单数

        double ordertotalbytime = 0.00;//得到本月已支付的订单总额

        double ordertotalbytime1 = 0.00;//该教师本月目前应得收益
        for (int i = 0; i < orderList.size(); i++) {
            String time1 = null;
            Date date1 = null;
            try {
                date1 = sdf1.parse(orderList.get(i).getOrderTime());
            }catch (ParseException e){
                e.printStackTrace();
            }

            String time = sdf.format(date1);
            //判断订单时间是否是本月时间
            int n = 0;
            if (day<=10){//每月10号发工资
                calendar.add(Calendar.MONTH, -1);
                time1 = sdf.format(calendar.getTime());//将当前时间转换为固定格式
            }
            if (day>10){
                time1 = sdf.format(date);
            }
            if (time.equals(time1)){//判断是不是符合条件的月的

                n = 1;
            }
            int m = 0;
            if (time.equals(sdf.format(date))){
                //计算符合当前月的订单数
                ordercountbytime = ordercountbytime + 1;
                m = 1;
            }

            //判断订单是否已经支付
            Integer state = orderList.get(i).getOrderState();
            if (state == 1){
                //计算已支付订单的总数
                ordercountbystate = ordercountbystate + 1;
                //计算已支付订单的总额
                ordertotal = ordertotal + orderList.get(i).getOrderTotal();

                if(n == 1){
                    //计算条件月已支付的订单总额
                    ordertotalbytime = ordertotalbytime + orderList.get(i).getOrderTotal();
                }
                if(m == 1){
                    //计算当前月已支付的订单数
                    ordercountbytime1 = ordercountbytime1 + 1;
                }
            }
        }

        if (ordertotalbytime <= 1000.00){
            ordertotalbytime1 = ordertotalbytime*0.9;
        }else if (ordertotalbytime<= 3000.00){
            ordertotalbytime1 = 1000.00*0.9+(ordertotalbytime - 1000.0)*0.85;
        }else if (ordertotalbytime<= 6000.00){
            ordertotalbytime1 = 1000.00*0.9+2000*0.85+(ordertotalbytime-3000.00)*0.8;
        }else if (ordertotalbytime > 6000.00){
            ordertotalbytime1 = 1000.00*0.9+2000*0.85+3000*0.8+(ordertotalbytime-6000.00)*0.7;
        }

        Map<String,Object> map = new HashMap<>();
        map.put("ordercount",ordercount);//订单总数
        map.put("ordercountbystate",ordercountbystate);//已支付订单总数
        map.put("ordertotal",ordertotal);//已支付的订单总额
        map.put("ordertotal1",ordertotal1);//该教师已得历史收益
        map.put("ordercountbytime",ordercountbytime);//得到当前月订单数
        map.put("ordercountbytime1",ordercountbytime1);//得到当前月已支付订单数
        map.put("ordertotalbytime1",ordertotalbytime1);//该教师本月目前应得收益
        return map;
    }
}
